library(plyr)
library(Hmisc)


setwd("~/Dropbox/China Huawei Paper/Production Materials/Replication/")
dat <- read.csv("dataFile_China_Huawei.csv")

dat$autocracy <- NA
dat$autocracy[dat$v2x_polyarchy>0.42] <- 0
dat$autocracy[dat$v2x_polyarchy<=0.42] <- 1

# Lagging variables
dat <- dat[order(dat$cabb, dat$year, decreasing=F),]
paneldata.lags <- function(X, unitvar, timevar, lagvars, lags=c(1)){
    stopifnot(length(lagvars)>=1)
    X <- X[order(X[,timevar]),] # just in case it's not sorted.

    innertask <- function(Y, lagvars, lags) {     
        E <- labels <- NULL
        for (v in lagvars) {
            for (i in lags) {E <- cbind(E, Lag(Y[,v], i))}
            labels <- c(labels, paste(v, ".l", lags, sep=""))
            }
        colnames(E) <- labels
        cbind(Y, E)
        }
        do.call("rbind", by(X, X[,unitvar], innertask, lagvars, lags)) 
    }
dat <- paneldata.lags(X=dat, unitvar="cabb", timevar="year", lagvars=c("china.oda", "china.oof", "china.military", "china.huawei", "icews.protest", "icews.repression", "v2x_polyarchy", "pres.election", "coup.attempts", "successful.coups", "GDPcur", "pcGDPcur", "electricitypc", "internetpen"))

who = ddply(dat, .(cabb, year), plyr::summarize, huawei=sum(china.huawei,na.rm=T), huawei.l1=sum(china.huawei.l1,na.rm=T), v2x_polyarchy=mean(v2x_polyarchy,na.rm=T), polity2=mean(polity2,na.rm=T), autocracy=mean(autocracy,na.rm=T), pcGDPcur=mean(pcGDPcur,na.rm=T), GDPcur=mean(GDPcur,na.rm=T), electricitypc=mean(electricitypc,na.rm=T), internetpen=mean(internetpen,na.rm=T), icews.protest=mean(icews.protest,na.rm=T), icews.repression=mean(icews.repression,na.rm=T), china.oda=mean(china.oda,na.rm=T), china.military=mean(china.military,na.rm=T), china.oof=mean(china.oof,na.rm=T), coup.attempts=mean(coup.attempts.l1,na.rm=T), pres.election=mean(pres.election,na.rm=T), pop=mean(pop,na.rm=T), resrentsGDP=mean(resrentsGDP,na.rm=T) )

out <- lm(log(huawei+1) ~ v2x_polyarchy + log(pcGDPcur) + electricitypc + log(china.oda+1) + log(pop) + pres.election + coup.attempts + resrentsGDP + as.factor(year), data=who, subset=pop>0); summary(out)

sY <- sd(log(who$huawei+1), na.rm=T)
sPoly <- sd(who$v2x_polyarchy, na.rm=T)
sGDPpc <- sd(log(who$pcGDPcur), na.rm=T)
sElec <- sd(who$electricitypc, na.rm=T)
sOda <- sd(log(who$china.oda+1), na.rm=T)
sPop <- sd(log(who$pop[who$pop>0]), na.rm=T)
sPres <- sd(who$pres.election, na.rm=T)
sCoup <- sd(who$coup.attempts, na.rm=T)
sRes <- sd(who$resrentsGDP, na.rm=T)

stand.out <- rbind(		c(coef(out)[2]*(sPoly/sY), confint(out)[2,]*(sPoly/sY)), 
						c(coef(out)[3]*(sGDPpc/sY), confint(out)[3,]*(sGDPpc/sY)), 
						c(coef(out)[4]*(sElec/sY), confint(out)[4,]*(sElec/sY)), 
						c(coef(out)[5]*(sOda/sY), confint(out)[5,]*(sOda/sY)), 
						c(coef(out)[6]*(sPop/sY), confint(out)[6,]*(sPop/sY)), 
						c(coef(out)[7]*(sPres/sY), confint(out)[7,]*(sPres/sY)),
						c(coef(out)[8]*(sCoup/sY), confint(out)[8,]*(sCoup/sY)),
						c(coef(out)[9]*(sRes/sY), confint(out)[9,]*(sRes/sY))
						)
rownames(stand.out) <- c("Polyarchy Index", "GDP Per Capita", "Electricity", "Chinese Aid", "Population", "Presidential\nElections", "Coup Attempts", "Natural Resources")
stand.out <- stand.out[order(stand.out[,1], decreasing=T),]

var.names <- row.names(stand.out)
n.coefs <- length(var.names)
par(mar=c(bottom=4.0,left=10.0,top=1.0,right=0.2), lheight=0.8,xpd=F) # xpd lets plot outside graph area
plot(0,0, xlim=c(-0.4,0.4), ylim=c(0.5,n.coefs+0.5), axes=F, col="White", main="Correlates of Huawei Transfers", cex.main=1.3, xlab="Standardized Coefficient", ylab="", cex.lab=1.3)
points(x=stand.out[,1], y=rev(seq(1,n.coefs,by=1)), col="black", pch=20, cex=2)
segments(x0=stand.out[,2], y0=rev(seq(1,n.coefs,by=1)), x1=stand.out[,3], y1=rev(seq(1,n.coefs,by=1)), lwd=1.5, bg=c("black"))
axis(1, at=seq(-0.4,0.4,by=0.1), line=0, las=0, tick=T, cex.axis=1.2)
axis(2, at=seq(1,n.coefs, by=1), label=rev(var.names), line=0, col="Black", col.axis="Black", col.ticks="Black", cex.axis=1.2, tick=F, las=2)
abline(v=0, lty=2)

